<?php
    /**
    * Send out emails to the club, from messages on the board
    */
    

    $groups = array('error','club','Polo','Surf','Whitewater','Social','Combos','Committee');
    
    class mailer
    {
        function mailPost($EID)
        {
            if ($post = mqs("SELECT Title,Text,MID,Type,TID FROM board WHERE EID=".$EID))
            {
              include('data/class.phpmailer.php');
              
              $content .= 'Composing emails..<br>';
              $mail = new PHPMailer();
              $mail->IsMail();
      
              $mail->From     = mqs("SELECT eMail FROM members WHERE MID=".$post['MID']);
              $mail->FromName = mqs("SELECT name FROM members WHERE MID=".$post['MID']);
              
              $mail->WordWrap = 50;
              $mail->IsHTML(true);
              
              $forum = $db_types[mqs("SELECT type FROM `board` WHERE `EID`=".$EID)];
              /* 'general' => 0, 'events' => 1, 'lost' => 2, 'buy' => 3, 'polo' => 4); */
              
              
              $subject = "UCCC-Forum:$forum email - ".$post['Title'];
              $mail->Subject  =  $subject;
              
              $mainBody = $post['Text'];   //Adds the text from the latest post to the mesasge.
              
              /* Add the rest of the thread. */
              if($posts = mq("SELECT EID,Date,Title,Text,Name,Pic FROM board b
                       INNER JOIN members m ON m.MID=b.MID
                       WHERE TID=".($TID+0)." ORDER BY EID"))
              {
                $num_posts = mysql_num_rows($posts);
                
              }
              for($i=1; i<=$num_posts;$i++)
              {
                  $currentpost = mysql_fetch_assoc($posts);
                  extract($currentpost);
                  $mainBody .= "<br><hr><br>";
                  $mainBody .= "<h2>'$Title' posted by: $Name on ".formatDate($Date)."</h2>";
                  $mainBody .= $Pic ? '<img class="avatar" src="./pics/members/'.$Pic.'" width="150" title="'.$Name.'" alt="'.$Name.'" >':"";
                  $mainBody .= $Text;
              }
                  
                  
                  
              
              $mail->Body     =  '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
                    <html>
                      <head>
                        <meta http-equiv="content-type" content="text/html;CHARSET=iso-8859-1">
                      </head>
                      <body style="background-color:#CCCCFF; color: #336699; font: 12pt Verdana, Arial; margin: 20px">
                      <b>'.$subject.'</b><br>
                      <br>'
                      .$mainBody
                      .'</body>
                    </html>';  
             
              $addispermail = 15;  $err = '';
              $query = "SELECT Name,eMail FROM members WHERE send_notes=1 AND MID=2"; 
              $type = mqs("SELECT type FROM `board` WHERE `EID`=".$EID);
              if($type==1){ $query .= ' AND news_river=1';}
              elseif($type==4){ $query .= ' AND news_polo=1';}
                            
              $cnt = 0; $it = 1; $success = true;
              if ($qres = mq($query)) 
              {
                  $content .= '<br>Sending to ' . mysql_numrows($qres) .' people...<br>';
                while($member = mysql_fetch_row($qres)) {
                    $mail->AddBCC($member[1],$member[0]); 
                    $cnt++;
                    if ($cnt % $addispermail == 0) {
                       $res = $mail->Send();
                       if (!$res) {
                            $err .= 'Error in iteration '.$it.': '.$mail->ErrorInfo.'<br>\n';
                            $success = false;
                            break;
                       }
                       $mail->ClearBCCs();
                       $cnt = 0;
                       $content .= 'Sent mails: '.($it-1)*$addispermail.' - '.$it*$addispermail.'<br>';
                       $it++;
                    }
                }
              }  #*/
              if ($success && $cnt > 1) 
              {
                  $res = $mail -> Send();
                  if (!$res) {
                            $err .= 'Error in iteration '.$it.': '.$mail->ErrorInfo.'<br>\n';
                            $success = false;
                  } else {
                      $content .= 'Sent mail '.($it-1)*$addispermail.' - '.(($it-1)*$addispermail+$cnt).' <br><br><hr />';
                  }
              }
              #if (false) {
              if ($success) {
                  logevent("Board emails sent sent out.");
              } else  { 
                  mq("DELETE FROM newsletter WHERE NID=".$NID);
                  mq("ALTER TABLE newsletter AUTO_INCREMENT=".$NID);
                  logevent('Error sending board email');
                  $content .= $err;
              }
            }
        }
    }
    $mailer = new mailer();
   
  
?>